package xin.zhiyin.控制.system.电子书;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.zhiyin.实体.书类目Entity;
import xin.zhiyin.工具.JthinkTree;
import xin.zhiyin.工具.ResponseBo;
import xin.zhiyin.工具.TreeUtils;
import xin.zhiyin.服务.书.书类目Service;

import java.util.*;

@Controller
//@RestController
@RequestMapping(path = "/system/ebook/category")//页面的url地址
public class 书类目Controller {
    @Autowired
    private 书类目Service 书类目Service;
    @GetMapping(path = "/list")
    public String 电子书类目(){
        return "system/ebook/category/list";
    }
    @RequestMapping("/EBookCategoryTreeList")
    @ResponseBody
    public ResponseBo getEBookCategoryTreeList() {
        List<JthinkTree<书类目Entity>> tree = new ArrayList<>();
        List<书类目Entity> terms = 书类目Service.获取所有书类目();
        if (null != terms) {
            buildTrees(tree, terms);
            JthinkTree<书类目Entity> termTree = TreeUtils.buildTree(tree);
            List<JthinkTree<书类目Entity>> nodes=new ArrayList<>();
            nodes.add(termTree);
            return ResponseBo.ok(nodes);
        }
        return ResponseBo.ok();
    }
    private void buildTrees(List<JthinkTree<书类目Entity>> tree, List<书类目Entity> terms) {
        terms.forEach(term -> {
            JthinkTree<书类目Entity> treeNode = new JthinkTree<>();
            treeNode.setTitle(term.getName());
            Long parentId = term.getParentId();
            treeNode.setId(term.getId().toString());
            if (null != parentId) {
                treeNode.setParentId(parentId.toString());
            }
            tree.add(treeNode);
        });
    }
    @PostMapping("/add")
    @ResponseBody
    public ResponseBo addEBookCategory(书类目Entity 类目) {
        if(StringUtils.isBlank(类目.getName())) {
            return ResponseBo.error("请输入类目名称");
        }
        书类目Service.保存(类目);
        return ResponseBo.ok("新增书类目成功");
    }
    @PostMapping("/update")
    @ResponseBody
    public ResponseBo updateEBookCategory(书类目Entity 类目) {
        书类目Service.更新(类目);
        return ResponseBo.ok("更新类目成功");
    }
    @RequestMapping("/listData")
    @ResponseBody
    public String getAllEBookCategory(String termName) {

        书类目Entity 类目;
        Iterator<书类目Entity> 类目Iter = 书类目Service.获取所有书类目().iterator();
        List<LinkedHashMap<String, Object>> 类目List = new ArrayList<>();
        while(类目Iter.hasNext())
        {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            类目 = 类目Iter.next();
            map.put("id",类目.getId());
            map.put("parentId",类目.getParentId());
            map.put("name",类目.getName());
            map.put("link",类目.getLink());
            map.put("coverImage",类目.getCoverImage());
            map.put("status",类目.getStatus());
            map.put("sort",类目.getSort());
            类目List.add(map);
        }
        String josn类目List = JSON.toJSONString(类目List);
//        model.addAttribute("systemMenuList",josn菜单List);
        return josn类目List;
    }
    @RequestMapping("/getEBookCategory")
    @ResponseBody
    public ResponseBo getEBookCategory(Long categoryId) {
        书类目Entity 类目 = 书类目Service.根据Id获取书类目(categoryId);
        return ResponseBo.ok(类目);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseBo deleteEBookCategory(String categoryIds) {
        List<String> ids = Arrays.asList(categoryIds.split(","));
        List<Long> LString = new ArrayList();
        for(String st:ids)
        {
            LString.add(Long.valueOf(st));
        }
        if(书类目Service.批量删除(LString)==false)
        {
            return ResponseBo.ok("此类目下还有书籍。请把书籍转移到其他类目，或者是全部清除之后有再来删除此类目！");
        }else{
            return ResponseBo.ok(LString);
        }
    }
}
